<?php

namespace app\admin\model\factor;

use think\Model;


class CarbonFactor extends Model
{

    // 定义关联的数据表名称
    protected $name = 'carbon_factor';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'lifecycle_text',
    ];

    protected $type = [
        'snapshot' => 'array',
        'images' => 'array',
        'createtime' => 'timestamp:Y-m-d H:i:s',
        'updatetime' => 'timestamp:Y-m-d H:i:s',
        'review_time' => 'timestamp:Y-m-d H:i:s',
        'authority_time' => 'timestamp:Y-m-d H:i:s',
        'account_time' => 'timestamp:Y-m-d H:i:s',
    ];


    // 关联因子收藏表
    public function collection()
    {
        return $this->hasMany(FactorCollection::class, 'id', 'factor_id');
    }

    //核算认证

    public function getExtraAttr($value)
    {
        return $value ? json_decode($value, true) : [];
    }

    public function getLastFactorReviewAttr()
    {
        return $this->factorReview()->field('id,factor_id,price,images,description,category_id,reason,createtime,checktime,status')->order('checktime', 'desc')->find();
    }

    public function factorReview()
    {
        return $this->hasMany(FactorReview::class, 'factor_id', 'id');
    }

    public function getLastAccountAuthAttr()
    {
        return $this->accountAuth()->field('id,factor_id,user_id,reason,createtime,checktime,status')->order('createtime', 'desc')->find();
    }

    public function accountAuth()
    {
        return $this->hasMany(AccountAuthentication::class, 'factor_id', 'id');
    }
    public function getLastAuthorityAuthAttr()
    {
        return $this->authorityAuth()->order('createtime', 'desc')->find();
    }

    public function authorityAuth()
    {
        return $this->hasMany(Authority::class, 'factor_id', 'id');
    }


    public function getSuccessAccountAuthAttr()
    {
        return $this->accountAuth()->where('status', 1)->order('checktime', 'desc')->find();
    }


    public function getCompanyNameAttr($value, $data)
    {

        if (isset($data['user_id'])) {
            if ($data['user_id'] == 0) return '平台';
            return \app\admin\model\User::where('id', $data['user_id'])->value('company_name') ?: '';
        }
        return '';

    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['createtime'] ?? "");
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getStatusList(): array
    {
        return ['0' => __('Hidden'), '1' => __('Normal'), '2' => __('Status rejected')];
    }

    public function getCheckStatusList()
    {
        return ['0' => __('Not_certified'), '1' => __('Certified'), '2' => __('Status rejected')];
    }

    public function getFactorSn(): string
    {
        $no_use = true;
        $sn = '';
        while ($no_use) {
            $sn = get_random_str(3, 14);
            if (!$this->where('sn', $sn)->find()) {
                $no_use = false;
            }
        }
        return $sn;
    }

    public function getSalesAttr($value, $data)
    {
        if ($value < 10000) return $value;
        return $value < 100000000 ? substr($value, 0, -4) . '万+' : substr($value, 0, -8) . '亿+';
    }

    /**
     * 转换生命周期读取内容
     */
    protected function getLifecycleTextAttr($value, $data)
    {
        $value = $value ?: ($data['lifecycle'] ?? '');
        return $this->getLifecycleType()[$value] ?? '';

    }

    public function getLifecycleType(): array
    {
        return [1 => '摇篮到大门', 2 => '摇篮到坟墓'];
    }

}
